//
//  SettingHomeViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/8/1.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "SettingHomeViewController.h"
#import "SettingTableViewCell.h"
#import "OtherH5ViewController.h"
#import "OpinionViewController.h"

@interface SettingHomeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SettingHomeViewController
- (IBAction)loginOutAction:(id)sender {
    [self showAlertWithTitleArray:@[@"取消",@"确定"] title:@"退出登录" message:@"是否确定退出登录？" alertStyle:UIAlertControllerStyleAlert];
    __block typeof(self) blockSelf = self;
    self.alertBlock = ^(NSString *actionTitle) {
        if ([actionTitle isEqualToString:@"确定"]) {
            [[UserInfo shareInstance] logout];
            [blockSelf disMissSelf];
        }
    };
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"系统设置";
    [self.tableView clearExtendCellLine];
}
#pragma mark - 网络请求
- (void)uploadMisreceive:(BOOL)isOn {
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:URL_Save_Misreceive parameters:@{@"misreceive":[NSString stringWithFormat:@"%d",isOn]} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            [UserInfo shareInstance].user.misreceive = isOn;
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 4;
        case 1:
            return 1;
        default:
            return 0;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SettingTableViewCell *cell = [SettingTableViewCell settingTableViewCellWith:tableView indexPath:indexPath];
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    [cell.secondTitleLabel setText:@"消息推送"];
                    /*
                     //MARK:测试
                     BOOL isOpen = YES;
                     if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0f) {
                     UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
                     if (UIUserNotificationTypeNone == setting.types) {
                     NSLog(@"推送关闭");
                     isOpen = NO;
                     }else{
                     NSLog(@"推送打开");
                     isOpen = YES;
                     }
                     }else{
                     UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
                     if(UIRemoteNotificationTypeNone == type){
                     NSLog(@"推送关闭");
                     isOpen = NO;
                     }else{
                     NSLog(@"推送打开");
                     isOpen = YES;
                     }
                     }
                     [cell.secondSwitch setOn:isOpen];
                     //MARK:-
                     */
                    [cell.secondSwitch setOn:[UserInfo shareInstance].user.misreceive];
                    cell.switchBlock = ^(BOOL isOn) {
                        //MARK:测试 设置是否接受消息接口
                        [self uploadMisreceive:isOn];
//                        if (isOn) {
//                            [[UIApplication sharedApplication] registerForRemoteNotifications];
//                        } else {
//                            [[UIApplication sharedApplication] unregisterForRemoteNotifications];
//                        }
                    };
                    break;
                }
                case 1:
                {
                    [cell.firstTitleLabel setText:@"意见反馈"];
                    break;
                }
                case 2:
                {
                    [cell.firstTitleLabel setText:@"使用帮助"];
                    break;
                }
                case 3:
                {
                    [cell.firstTitleLabel setText:@"联系我们"];
                    break;
                }
                    //        case 4:
                    //        {
                    //            [cell.firstTitleLabel setText:@"退出登录"];
                    //            break;
                    //        }
                default:
                    break;
            }
        } break;
        case 1:
        {
            [cell.thirdNumberLabel setText:[NSString stringWithFormat:@"v%@",AppVersion]];
        } break;
        default:
            break;
    }
    
    
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, 5)];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    dispatch_async(dispatch_get_main_queue(), ^{
        switch (indexPath.section) {
            case 0:
            {
                switch (indexPath.row) {
                    case 1:
                    {
                        //意见反馈
                        OpinionViewController *opinionVC = [[OpinionViewController alloc] init];
                        [self.navigationController pushViewController:opinionVC animated:YES];
                        break;
                    }
                    case 2:
                    {
                        //使用帮助
                        OtherH5ViewController *h5VC = [[OtherH5ViewController alloc] init];
                        h5VC.title = @"使用帮助";
                        h5VC.type = 1;
                        [self.navigationController pushViewController:h5VC animated:YES];
                        break;
                    }
                    case 3:
                    {
                        //联系我们
                        OtherH5ViewController *h5VC = [[OtherH5ViewController alloc] init];
                        h5VC.title = @"联系我们";
                        h5VC.type = 2;
                        [self.navigationController pushViewController:h5VC animated:YES];
                        break;
                    }
                        //            case 4:
                        //            {
                        //                //退出登录
                        //                [self showAlertWithTitleArray:@[@"取消",@"确定"] title:@"退出登录" message:@"是否确定退出登录？" alertStyle:UIAlertControllerStyleAlert];
                        //                __block typeof(self) blockSelf = self;
                        //                self.alertBlock = ^(NSString *actionTitle) {
                        //                    if ([actionTitle isEqualToString:@"确定"]) {
                        //                        [[UserInfo shareInstance] logout];
                        //                        [blockSelf disMissSelf];
                        //                    }
                        //                };
                        //                break;
                        //            }
                    default:
                        break;
                }
            } break;
                
            default:
                break;
        }
        
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
